package cn.onein.edu.web.jms.producer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.MessageCreator;

import cn.onein.edu.common.MailType;
import cn.onein.edu.common.base.JmsSupport;

public class SendMailProducer extends JmsSupport{
	
	/**
	 * 注册新用户时发送邮件的MQ消息
	 * @param toEmail
	 */
	public void sendRegisterMailMessage(final String toEmail){
		if(toEmail == null || "".equals(toEmail.trim())){
			throw new IllegalArgumentException("注册新用户时发送邮件的MQ消息时，用户email地址不能为空！");
		}
		
		getJmsTemplate().send(getDestination(), new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {

				Message message = session.createMessage();

				//设置邮件类型
				message.setStringProperty(MSG_KEY_SEND_MAIL_TYPE,MailType.Register.toString());
				//设置用户email地址
				message.setStringProperty(MSG_KEY_USER_EMAIL, toEmail);

				return message;
			}
		});
	}
	
	/**
	 * 意见反馈发送邮件的MQ消息
	 */
	public void sendFeedBackMessage(final String title,final String mobile,final String email,final String content){
		if(content == null || "".equals(content.trim())){
			throw new IllegalArgumentException("发送用户意见的MQ消息时，用户意见内容不能为空！");
		}
		
		getJmsTemplate().send(getDestination(), new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {

				Message message = session.createMessage();

				//设置邮件类型
				message.setStringProperty(MSG_KEY_SEND_MAIL_TYPE, MailType.FeedBack.toString());
				
				String tStr = title;
				String mStr = mobile;
				
				if(title == null || "".equals(title.trim())){
					tStr = "意见反馈";
				}
				if(mobile == null || "".equals(mobile.trim())){
					mStr = "无";
				}
				message.setStringProperty(MSG_KEY_FEEDBACK_TITLE, tStr);
				
				message.setStringProperty(MSG_KEY_FEEDBACK_MOBILE, mStr);
				
				message.setStringProperty(MSG_KEY_FEEDBACK_EMAIL, email);
				
				message.setStringProperty(MSG_KEY_FEEDBACK_CONTENT, content);

				return message;
			}
		});
	}
	
	/**
	 * 密码找回时发送邮件的MQ消息
	 * @param toEmail
	 * @param newPassword
	 */
	public void sendPasswordRetrieveMessage(final String toEmail, final String newPassword){
		if(toEmail == null || "".equals(toEmail.trim())){
			throw new IllegalArgumentException("密码找回时发送邮件的MQ消息时，用户email地址不能为空！");
		}
		
		if(newPassword == null || "".equals(newPassword.trim())){
			throw new IllegalArgumentException("密码找回时发送邮件的MQ消息时，用户密码不能为空！");
		}
		
		getJmsTemplate().send(getDestination(), new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {

				Message message = session.createMessage();

				//设置邮件类型
				message.setStringProperty(MSG_KEY_SEND_MAIL_TYPE,MailType.PasswordRetrieve.toString());
				//设置用户email地址
				message.setStringProperty(MSG_KEY_USER_EMAIL, toEmail);
				//设置用户新密码
				message.setStringProperty(MSG_KEY_USER_NEW_PASS, newPassword);
				
				return message;
			}
		});
	}
	
	/**
	 * 发送修改eMail的MQ消息
	 * @param toEmail
	 * @param sharedActionUrl
	 */
	public void sendChangeEMailUrlMessage(final String toEmail, final String changeEmailUrl){
		if(toEmail == null || "".equals(toEmail.trim())){
			throw new IllegalArgumentException("发送修改eMail的MQ消息时，用户email地址不能为空！");
		}
		
		if(changeEmailUrl == null || "".equals(changeEmailUrl.trim())){
			throw new IllegalArgumentException("发送修改eMail的MQ消息时，改变email的URL不能为空！");
		}
		
		getJmsTemplate().send(getDestination(), new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {

				Message message = session.createMessage();

				//设置邮件类型
				message.setStringProperty(MSG_KEY_SEND_MAIL_TYPE,MailType.ChangeEmail.toString());
				//设置用户email地址
				message.setStringProperty(MSG_KEY_USER_EMAIL, toEmail);
				//设置改变email的URL
				message.setStringProperty(MSG_KEY_CHANGE_EMAIL_URL, changeEmailUrl);
				
				return message;
			}
		});
	}
	
	/**
	 * 发送分享行动指南邮件的MQ消息
	 * @param toEmail
	 * @param sharedActionUrl
	 */
	public void sendShareActionMailMessage(final String toEmail, final String sharedActionUrl){
		if(toEmail == null || "".equals(toEmail.trim())){
			throw new IllegalArgumentException("发送分享行动指南邮件的MQ消息时，用户email地址不能为空！");
		}
		
		if(sharedActionUrl == null || "".equals(sharedActionUrl.trim())){
			throw new IllegalArgumentException("发送分享行动指南邮件的MQ消息时，分享的URL不能为空！");
		}
		
		getJmsTemplate().send(getDestination(), new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {

				Message message = session.createMessage();

				//设置邮件类型
				message.setStringProperty(MSG_KEY_SEND_MAIL_TYPE,MailType.ShareAction.toString());
				//设置用户email地址
				message.setStringProperty(MSG_KEY_USER_EMAIL, toEmail);
				//设置分享的URL
				message.setStringProperty(MSG_KEY_SHARED_URL, sharedActionUrl);
				
				return message;
			}
		});
	}
}
